package zl.ordermanager.controller;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import zl.ordermanager.service.CustomerVisitService;
import zl.ordermanager.vo.CustomerVisit;
import zl.ordermanager.vo.DataGrid;
import zl.ordermanager.vo.EContactResult;
import zl.ordermanager.vo.ECustomerType;
import zl.ordermanager.vo.PageHelper;
import zl.ordermanager.vo.Total;

@Controller
@RequestMapping("/customerVisit")
public class CustomerVisitController extends BaseController {

	@Autowired
	private CustomerVisitService customerVisitService;

	@RequestMapping("/showList")
	public String showList(HttpSession session, PageHelper pageHelper, Model model) {
		Total total = new Total();
		DataGrid<CustomerVisit> dataGrid = customerVisitService.dataGrid(session, pageHelper, total);
		List<CustomerVisit> customerVisitList = dataGrid.getList();
		long totalCount = dataGrid.getTotal();

		model.addAttribute("customerVisitList", customerVisitList);
		model.addAttribute("totalCount", totalCount);
		model.addAttribute("pageHelper", pageHelper);
		model.addAttribute("total", total);
		model.addAttribute("resultList", EContactResult.values());

		return "customerVisitList";
	}

	@RequestMapping("/showAdd")
	public String showAdd(HttpSession session, Model model) {
		model.addAttribute("customerTypeList", ECustomerType.values());
		model.addAttribute("resultList", EContactResult.values());
		model.addAttribute("currentDate", new SimpleDateFormat("yyyy-M-d").format(new Date()));

		return "customerVisitAdd";
	}

	@RequestMapping(value = "/add", method = RequestMethod.POST)
	public ModelAndView add(CustomerVisit customerVisit) {
		try {
			customerVisitService.add(customerVisit);
		} catch (Exception e) {
			return ajaxDoneError(e.getMessage());
		}

		return ajaxDoneSuccess("操作成功");
	}

	@RequestMapping("/showEdit/{customerVisitId}")
	public String showEdit(@PathVariable("customerVisitId") Integer customerVisitId, Model model) {
		CustomerVisit customerVisit = customerVisitService.get(customerVisitId);

		model.addAttribute("customerVisit", customerVisit);
		model.addAttribute("customerTypeList", ECustomerType.values());
		model.addAttribute("resultList", EContactResult.values());

		return "customerVisitEdit";
	}

	@RequestMapping(value = "/edit", method = RequestMethod.POST)
	public ModelAndView edit(CustomerVisit customerVisit) {
		try {
			customerVisitService.edit(customerVisit);
		} catch (Exception e) {
			return ajaxDoneError(e.getMessage());
		}
		return ajaxDoneSuccess("操作成功");
	}

	@RequestMapping("/delete/{customerVisitId}")
	public ModelAndView delete(@PathVariable("customerVisitId") Integer customerVisitId) {

		customerVisitService.delete(customerVisitId);

		return ajaxDoneSuccess("操作成功");
	}
}
